package com.starsea.book.ui.newpage.activity

import android.os.Bundle
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.BarUtils
import com.starsea.book.R
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityAccountAddEditBinding
import com.starsea.book.databinding.ActivityBookRanksBinding
import com.starsea.book.ui.adapter.MyFragmentStateAdapter
import com.starsea.book.ui.book.search.SearchActivity
import com.starsea.book.ui.newpage.adapter.BookRanksTitleAdapter
import com.starsea.book.ui.newpage.entity.BookTabTitleEntity
import com.starsea.book.ui.newpage.fragment.BookDepositoryFragment
import com.starsea.book.ui.newpage.fragment.HomeChildFragment
import com.starsea.book.ui.newpage.viewmodel.HomeViewModel
import com.starsea.book.ui.newpage.viewmodel.WalletViewModel
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.viewbindingdelegate.viewBinding
import com.starsea.book.utils.visible

/**
 * @author Lee
 * @desc : 书库
 */
class BookDepositoryActivity : VMBaseActivity<ActivityBookRanksBinding, HomeViewModel>() {

    lateinit var mAdapter: BookRanksTitleAdapter

    private lateinit var mFragmentAdapter: MyFragmentStateAdapter

    var mFragentList: MutableList<Fragment> = arrayListOf()

    override val binding by viewBinding(ActivityBookRanksBinding::inflate)
    override val viewModel by viewModels<HomeViewModel>()

    override
    fun onActivityCreated(savedInstanceState: Bundle?) {
        BarUtils.addMarginTopEqualStatusBarHeight(binding.llTitle)
        binding.ivSearch.setOnClickListener {
            startActivity<SearchActivity>()
        }
        binding.ivSearch.visible()

        mAdapter = BookRanksTitleAdapter(this)
        binding.rvTitle.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        binding.rvTitle.adapter = mAdapter



        viewModel.getHomeTopTitle()

        viewModel.topTitleResutl.observe(this) {
            when (it.status) {
                200 -> {

                    mFragentList = arrayListOf()
                    it.data!!.forEach {
                        mFragentList.add(BookDepositoryFragment.newInstance(it.value))
                    }
                    mAdapter.submitList(it.data)
                    mFragmentAdapter = MyFragmentStateAdapter(mFragentList, this)
                    binding.vpContent.adapter = mFragmentAdapter
//                    binding.vpContent.isUserInputEnabled = false
                    binding.vpContent.offscreenPageLimit = mFragentList.size

                    mAdapter.addOnItemChildClickListener(R.id.tv_title) { _, _, position ->
                        mAdapter.items.forEach { it.isSelect = false }
                        mAdapter.getItem(position = position)!!.isSelect = true
                        mAdapter.notifyDataSetChanged()
                        binding.vpContent.currentItem = position
                    }

                    binding.vpContent.registerOnPageChangeCallback(object :
                        ViewPager2.OnPageChangeCallback() {
                        override fun onPageSelected(position: Int) {
                            super.onPageSelected(position)
                            mAdapter.items.forEach { it.isSelect = false }
                            mAdapter.getItem(position = position)!!.isSelect = true
                            mAdapter.notifyDataSetChanged()
                        }
                    })
                }

                else -> {

                }
            }
        }
    }
}